/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import ActionForm.*;
import bean.*;
import java.util.*;
import java.util.ArrayList;
/**
 *
 * @author cpn02
 */
public class DeleteCartAction extends Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        // tach chuoi cac ID duoc truyen tu form(cac ID fai cach nhau bang dau ,) =>IDs, xoa cac product trong carts co ID trong IDs
        DeleteCartForm f = (DeleteCartForm)form;
        String IDstr = f.getIDs();
        if(IDstr==null||(IDstr.trim() == null ? "" == null : IDstr.trim().equals("")))
        {
            return mapping.findForward("success");
        }
        String[] aIDs = IDstr.split(",");
        ArrayList IDs = new ArrayList();
        for(int i =0;i<aIDs.length;i++)
            IDs.add(Long.parseLong(aIDs[i]));
        ArrayList<CartBean> carts = (ArrayList<CartBean>)request.getSession().getAttribute("carts");
        Iterator<CartBean> it = carts.iterator();
        CartBean cart;
        ArrayList<CartBean> tem = new ArrayList<CartBean>();
        while(it.hasNext())
        {
            cart = it.next();
            if(IDs.contains(cart.getPro().getIDpro()))
            tem.add(cart);
        }
        carts.removeAll(tem);
        return mapping.findForward("success");
    }


}
